/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.firstlife.mps.unittests;

import j2meunit.framework.Test;
import j2meunit.framework.TestCase;
import j2meunit.framework.TestMethod;
import j2meunit.framework.TestSuite;
import java.util.Random;

import com.aroundj2me.model.Communicator;
import com.aroundj2me.model.Constants;
import com.aroundj2me.xml.XMLDecoder;
import com.aroundj2me.xml.XMLEncoder;

public class ServerSecurityTest extends TestCase {
	private Communicator comm;
	private XMLEncoder encoder;
	private XMLDecoder decoder;
	
	public ServerSecurityTest() {
	}
	
	public ServerSecurityTest(String string, TestMethod method) {
		super(string, method);
	}
	
	protected void setUp() throws Exception {
		comm = new Communicator("http://localhost:8080/aroundME/server");
		encoder = new XMLEncoder();
		decoder = new XMLDecoder();
	}

	protected void tearDown() throws Exception {
		comm = null;
		encoder = null;
		decoder = null;
	}
	
	/**
	 * Tests a login with a random password and username
	 * The login should break, i.e. be rejected by the server
	 * and not succeed.
	 *
	 */
	public void testLoginServer_BREAK(){
		Random r = new Random();
		String username="juri" + r.nextLong();
		String password = "" + r.nextLong();
		
		//simulate a place-retrieve request in which the authentication piece will be included
		String xml = encoder.encodeRequest(Constants.PARAM_RETRIEVE_GLOBAL,0,999999, username, password);
		comm.setRequestXML(xml);
		comm.run();
		
		String returnedXML = comm.getResponseXML();
		assertNotNull(returnedXML);
		
		decoder.setXmlResponseString(returnedXML);
		try {
			decoder.parseXML();
		} catch (Exception e) {
			e.printStackTrace();
			fail(e.getMessage());
		}
		
		assertEquals("type of response should be an alert", Constants.ALERT, decoder.getType());
		assertEquals("Type of the alert should be error", Constants.ALERT_ERROR, decoder.getAlert().getTitle());
	}
	
	public void testLoginServer_SUCCESS(){
		String username="kito";
		String password = "mypass";
		
		//simulate a place-retrieve request in which the authentication piece will be included
		String xml = encoder.encodeRequest(Constants.PARAM_RETRIEVE_GLOBAL,0,999999, username, password);
		comm.setRequestXML(xml);
		comm.run();
		
		String returnedXML = comm.getResponseXML();
		assertNotNull(returnedXML);
		decoder.setXmlResponseString(returnedXML);
		try {
			decoder.parseXML();
		} catch (Exception e) {
			e.printStackTrace();
			fail(e.getMessage());
		}
		
		assertTrue("type of response should be either alert type (info) or retrieve type", decoder.getType().equals(Constants.DATA_PLACE) || (decoder.getType().equals(Constants.ALERT) && decoder.getAlert().getTitle().equals(Constants.ALERT_INFO)));
	}
	
	public Test suite() {
		TestSuite suite = new TestSuite();
		suite.addTest(new ServerSecurityTest("ServerSecurityTest:testLoginServer_BREAK", new TestMethod() {
			public void run(TestCase tc) {
				((ServerSecurityTest) tc).testLoginServer_BREAK();
			}
		}));
		suite.addTest(new ServerSecurityTest("ServerSecurityTest:testLoginServer_SUCCESS", new TestMethod() {
			public void run(TestCase tc) {
				((ServerSecurityTest) tc).testLoginServer_SUCCESS();
			}
		}));
		
		return suite;
	}
}
